package jobflow.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import jobflow.entity.Departman;

import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Synchronized;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;


@Name("departmanDao")
@Scope(ScopeType.CONVERSATION)
@Synchronized
public class DepartmanDao implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2386902108899852909L;

	@In
	EntityManager entityManager;

	@In
	FacesMessages facesMessages;

	
	private String encodedImage=null;

	private Departman departman = new Departman();
	
	private Departman departman2 = new Departman();

	private List<Departman> departmanList = new ArrayList<Departman>();
    
	@Out(required=false,scope=ScopeType.CONVERSATION,value="currentRow")
	private Departman currentRow;

	@Create
	@Begin
	public void begin() {

		System.out.println("DepartmanDao calisti");

	}
	


	public void listener(UploadEvent event) throws Exception {
		UploadItem item = event.getUploadItem();	
		departman.setData(item.getData());	
	}

	public void departmanEkle() {

		entityManager.persist(departman);
		departman = new Departman();

		facesMessages.addFromResourceBundle("departman.eklenmisitir");
        



	
	}

	public void departmanSil() {

		entityManager.remove(currentRow);

		departmanList.remove(currentRow);
		
	
		   File file = new File("E:/worspaceler/seam_calisma/seam_calisma/jobflow/WebContent"+currentRow.getImage());
		      if(file.exists()){
		    	file.delete();
		      }
		
		      

		facesMessages.addFromResourceBundle("departman.sil");
	}

	@SuppressWarnings("unchecked")
	public void departmansList() throws Exception {

		departmanList = entityManager.createQuery("from Departman d")
				.getResultList();
		
		int i =0;
		
		for (Departman departmanResim : departmanList) {   
			
			i++;
		   
			String path ="E:/worspaceler/seam_calisma/seam_calisma/jobflow" +
		    		"/WebContent/images/departman/"+i+".jpeg";
			
		    File of = new File(path); 
		  
		    FileOutputStream osfd = new FileOutputStream(of);
		    if(departmanResim.getData()!=null){
		    osfd.write(departmanResim.getData());  
		    osfd.flush();
		    osfd.close();
		    }
		    path = "/images/departman/"+i+".jpeg";
		    
		   
		    departmanResim.setImage(path);
		    
		   

		}

	}

	public void temizle() {

		departman = new Departman();

		departmanList.clear();

		entityManager.clear();

	}

	public void pasif(Departman departman) {

		entityManager.createQuery("update Departman d set d.aktif=0")
				.executeUpdate();

		entityManager.refresh(departman);

	}

	public void aktif(Departman departman) {
		entityManager.createQuery("update Departman d set d.aktif=1")
				.executeUpdate();
		entityManager.refresh(departman);

	}


	
	public void departmanGuncel(Departman departman) {

		Departman depart = entityManager.find(Departman.class,
				departman.getId());

		this.departman2=depart;
	}
	
	
	public void departmanGuncellemeNow() {
		
		
		entityManager.createQuery("update Departman d set d.departmanAdi=:departmanAdi,d.departmanSorumlusu=:departmanSorumlusu,d.dahiliTel=:dahiliTel where d.id=:id").setParameter("id", departman2.getId())
		.setParameter("departmanAdi", departman2.getDepartmanAdi()).setParameter("departmanSorumlusu", departman2.getDepartmanSorumlusu()).setParameter("dahiliTel", departman2.getDahiliTel())		
		.executeUpdate();
           
     entityManager.refresh(departman2);
     
     

	}

	


	
	@SuppressWarnings("unchecked")
	public void departmanAra() throws IOException {

		entityManager.clear();

		String strSQL = "from Departman d where 1=1";

		if (!StringUtils.isEmpty(departman.getDepartmanAdi())) {
			strSQL += " and d.departmanAdi=:departmanAdi";
		}
		if (!StringUtils.isEmpty(departman.getDepartmanSorumlusu())) {
			strSQL += " and d.departmanSorumlusu=:departmanSorumlusu";
		}
		if (!StringUtils.isEmpty(departman.getDahiliTel())) {
			strSQL += " and d.dahiliTel=:dahiliTel";
		}
		if (departman.getAktif() != null) {
			strSQL += " and d.aktif=:aktif";
		}

		Query query = entityManager.createQuery(strSQL);

		if (!StringUtils.isEmpty(departman.getDepartmanAdi())) {
			query.setParameter("departmanAdi", departman.getDepartmanAdi());
		}
		if (!StringUtils.isEmpty(departman.getDepartmanSorumlusu())) {
			query.setParameter("departmanSorumlusu",
					departman.getDepartmanSorumlusu());
		}
		if (!StringUtils.isEmpty(departman.getDahiliTel())) {
			query.setParameter("dahiliTel", departman.getDahiliTel());
		}
		if (departman.getAktif() != null) {
			query.setParameter("aktif", departman.getAktif());

		}

		departmanList = query.getResultList();
		int i =0;
       for (Departman departmanResim : departmanList) {   
			
			i++;
		   
			String path ="E:/worspaceler/seam_calisma/seam_calisma/jobflow" +
		    		"/WebContent/images/departman/"+i+".jpeg";
			
		    File of = new File(path); 
		  
		    FileOutputStream osfd = new FileOutputStream(of);
		    if(departmanResim.getData()!=null){
		    osfd.write(departmanResim.getData());  
		    osfd.flush();
		    osfd.close();
		    }
		    path = "/images/departman/"+i+".jpeg";
		    
		   
		    departmanResim.setImage(path);
		    
		   

		}

	}

	public String geri() {

		departmanList.clear();
		return "/departmanAra.xhtml";

	}

	public void setDepartman(Departman departman) {
		this.departman = departman;
	}

	public Departman getDepartman() {
		return departman;
	}

	public void setDepartmanList(List<Departman> departmanList) {
		this.departmanList = departmanList;
	}

	public List<Departman> getDepartmanList() {
		return departmanList;
	}

	public void setCurrentRow(Departman currentRow) {
		this.currentRow = currentRow;
	}

	public Departman getCurrentRow() {
		return currentRow;
	}

	public void setEncodedImage(String encodedImage) {
		this.encodedImage = encodedImage;
	}

	public String getEncodedImage() {
		return encodedImage;
	}


	
	public static void main(String[] args) {
			
		
	
	}

	public void setDepartman2(Departman departman2) {
		this.departman2 = departman2;
	}

	public Departman getDepartman2() {
		return departman2;
	}

}
